package com.king.juc.atomicdemo.account;

/**
 * 保护共享资源: 加锁安全案例
 * @author king
 * @date 2020/11/29 16:53
 */
public class AccountSafe implements Account {

    private Integer balance;

    public AccountSafe(Integer balance) {
        synchronized (this) {
            this.balance = balance;
        }
    }

    @Override
    public Integer getBalance() {
        return balance;
    }

    @Override
    public void withdraw(Integer amount) {
        synchronized (this) {
            this.balance -= amount;
        }
    }
}
